package com.blog.wu.myblog.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;
import org.springframework.boot.autoconfigure.jms.JmsProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.DeliveryMode;
import javax.jms.Queue;

/**
 * @Author wuyanshen
 * @Date 2018-12-25 3:45 PM
 * @Discription 描述
 */
@Configuration
public class ActiveMqConfig {

    @Value("${queue}")
    private String queue;

    @Bean
    public Queue getQueue() {
        return new ActiveMQQueue(queue);
    }

    @Bean
    public JmsTemplate getJmsTemplate(ActiveMQConnectionFactory factory, Queue queue) {
        JmsTemplate jmsTemplate = new JmsTemplate();
        //进行持久化
        jmsTemplate.setDeliveryMode(DeliveryMode.PERSISTENT);
        jmsTemplate.setConnectionFactory(factory);
        jmsTemplate.setDefaultDestination(queue);
        //客户端签收模式：事物签收
        jmsTemplate.setSessionAcknowledgeMode(4);

        return jmsTemplate;
    }


    /**
     * 定义一个消息监听器连接工厂，这里定义点对点模式的监听器连接工厂
     */
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(ActiveMQConnectionFactory activeMQConnectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(activeMQConnectionFactory);
        /**设置连接数*/
        factory.setConcurrency("1-10");
        /**设置重连间隔时间*/
        factory.setRecoveryInterval(1000L);
        /**设置签收模式：事物型签收*/
        factory.setSessionAcknowledgeMode(4);

        return factory;
    }


}
